package com.gkwave.util;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailSender
{

	private Properties props ;
	
	private static String MAIL_PROPERTIE_FILE = "email.properties" ;
	private static String MAIL_SMTP_SENDER = "smartc@gkwave.com";
	private static String MAIL_SMTP_SENDER_PERSIONAL = "ADMIN.SMARTC";
	private static String MAIL_SMTP_AUTHENTICATION = "gkwave.com";
	private String  emailSenderId;
	private String emailPassword;
	public EmailSender() {
		init();
		// TODO Auto-generated constructor stub
	}
	void init() {
      props = new Properties();
      props = new PropertiesReader(MAIL_PROPERTIE_FILE).getProperties();
	}
	
	public boolean sendTextMessage(String mailSubject, String mailContent, String[] mailRecipients, boolean isHTML){
		boolean returnValue = false;
		Session session = getPasswordAuthentication();
		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(emailSenderId,MAIL_SMTP_SENDER_PERSIONAL));
			
	        InternetAddress addressTo[] = new InternetAddress[mailRecipients.length];
	        for(int i = 0; i < mailRecipients.length; i++)
	            addressTo[i] = new InternetAddress(mailRecipients[i]);
	        
			message.setRecipients(Message.RecipientType.TO, addressTo);
			message.setSubject(mailSubject);
			
			if(isHTML){
				message.setContent(mailContent, "text/html; charset=ISO-8859-1");
			}else{
				message.setText(mailContent);
			}
			Transport.send(message);
 
			System.out.println("Email sent....");
			returnValue = true;
 
		} catch (MessagingException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}		
		return returnValue;
	}
	
	private Session getPasswordAuthentication(){
		emailSenderId = props.getProperty("mail.smtp.sender");
		emailPassword = props.getProperty("mail.smtp.authentication");
		if(emailSenderId==null && emailPassword==null){
			emailSenderId = MAIL_SMTP_SENDER;
			emailPassword = MAIL_SMTP_AUTHENTICATION;
		}
		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(emailSenderId, emailPassword);
					}
				});
		return session;
	}
	
	public boolean sendHTMLMessage(String mailSubject, String mailContent, String[] mailRecipients){
		mailContent = new EmailMessageBuilder("./src/email.html.template").getMessageContent("Test", "Test", "Test").toString();
		return this.sendTextMessage(mailSubject, mailContent, mailRecipients, true);
	}
}
